layui.define(['engine_form', 'form', 'http', 'public', 'drawer'], function (exports) {
    let STR_FORMDESIGN = 'DesignForm';
    let ARR_CONFIG = {
        modeid: 0,
        menu_id: 0,
        type: 1,
        current: {},
    };
    let GetConfig = function () {
        return ARR_CONFIG;
    }
    let Init = function (modeid = '') {
        ARR_CONFIG.modeid = layui.public.GetQuery('id') || 0;
        if (modeid) {
            ARR_CONFIG.modeid = modeid;
        }
        let URL = '?id=' + ARR_CONFIG.modeid;
        history.pushState(null, null, URL);
        layui.http.SendAjax('/ruinuo/design.lists/index', {modeid: ARR_CONFIG.modeid}, [], function (res) {
            let data = res.data || [];

            layui.public.ViewTpl(data, 'view_body_data', 'view_body');
            layui.public.DropdownInit('.design-left .ruinuo_more');
            let title = data.title || '';
            if (title) {
                $('.design_head .title').html('列表设计器<i class="layui-icon layui-icon-right"></i><strong style="color: var(--theme_color);">' + title + '</strong>');
            } else {
                ARR_CONFIG.modeid = 0;
                history.pushState(null, null, '?id=0');
            }
            if (modeid =='') {
                ModeList();
            } else {
                ClickMenu();
            }
        })
    };

    let ModeList = function () {
        layui.http.SendAjax('/ruinuo/design.form/modelist', [], [], function (res) {
            let data = res.data || [];
            layui.public.ViewTpl(data, 'view_mode_list_data', 'view_mode_list');
            ClickMenu();
        })
    }
    let ClickMenu = function () {
        let menu_id = ARR_CONFIG.menu_id;
        let modeid = ARR_CONFIG.modeid;
        let click = '';
        let elem = '';
        $('.design-left .layui-menu ul li').each(function (b) {
            if ($(this).attr('data-id') == menu_id) {
                click = $(this);
                elem = $(this).parent('ul').parent('.layui-menu-item-group');
            }
        })
        if (click == "") {
            click = $('.design-left .layui-menu ul li').eq(0);
            elem = $('.design-left .layui-menu .layui-menu-item-group');
        }
        $(click).click();
        $(elem).attr('class', 'layui-menu-item-group layui-menu-item-down');
        $(elem).find("i").attr('class', 'layui-icon layui-icon-up');
    }

    let TableFind = function (menu_id = '') {
        ARR_CONFIG.menu_id = menu_id;
        Design();
    }

    let Design = function (type = 1) {

        $(".design-tab li").removeClass('layui-this');
        $(".design-tab li").eq(0).addClass('layui-this');
        if (type == 2) {
            layui.public.OpenWindow('/ruinuo/button/?mode_id=' + ARR_CONFIG.modeid, '按钮设置');
            return;
        }

        if (type) {
            ARR_CONFIG.type = type;
        }
        let data = {
            type: ARR_CONFIG.type,
            menu_id: ARR_CONFIG.menu_id,
        };
        layui.http.SendAjax('/ruinuo/design.lists/tabledata', data, [], function (res) {
            let back = res.data || [];
            let tableid = 'design_table_elem';
            let data = back.value || [];
            let cols = back.cols || [];
            let caozuo = [{
                title: '<div class="table_add" title="新增"  onclick="layui.design_table.AddTable()"><i class="layui-icon layui-icon-add-1"></i></div>',
                minWidth: 110,
                tpl: '<div><button type="button" class="pear-btn pear-btn-danger pear-btn-smm" onclick="layui.engine_table.DeleteTable(\'' + tableid + '\',{{d.LAY_INDEX}})"><i class="layui-icon layui-icon-delete"></i></button><button type="button" class="pear-btn pear-btn-primary pear-btn-smm ruinuo-drag"><i class="layui-icon layui-icon-slider"></i></button></div>'
            }];
            for (let i = 0; i < cols.length; i++) {
                caozuo.push(cols[i]);
            }
            let table = {
                style: 1,
                elem: tableid,
                limit: 0,
                page: false,
                source: back.source || [],
                cols: layui.engine_table.ColsAll(caozuo, tableid),
            }
            SettingData[tableid] = table;
            layui.engine_table.TableStyle(table, data);
            FieldChecked(back.field)
        })
    }


    let OpenAdd = function (type = 1) {
        let modeid = ARR_CONFIG.modeid || 0;
        if (modeid == 0) {
            layer.msg('请先选择模块', {icon: 2});
            layui.public.DrawerTpl('.mode_list', '300px', '模块类别');
            return false;
        }
        let url = '/ruinuo/lists/add/?mode_id=' + ARR_CONFIG.modeid;
        layui.public.OpenWindow(url, '新增菜单')
    }

    let AddTable = function () {
        var type = ARR_CONFIG.type || 1;
        switch (type) {
            case 1:
                layui.public.DrawerTpl('.field-layer-right', '400px', '模块类别')
                break;
            case 2:
                break;
            case 3:
                break;
        }
    }
    let FieldChecked = function (list = []) {

        let FiledStr = '';
        let table_filed_html = "";
        for (let i = 0; i < list.length; i++) {
            table_filed_html += '<tr>';
            table_filed_html += '<td colspan="3"><strong>' + list[i].value + '(' + list[i].label + ')</strong></td>';
            table_filed_html += '</tr>';
            let child = list[i].children;
            for (let ii = 0; ii < child.length; ii++) {
                let checked = '';
                if (child[ii].on) {
                    checked = 'checked';
                }
                table_filed_html += '<tr>';
                table_filed_html += '<td align="right"><input name="table_field_name" class="field_val_' + child[ii].value + '"  type="checkbox" ' + checked + ' data-name="' + child[ii].label + '" value="' + child[ii].value + '" lay-filter="table_field" lay-skin="primary"></td>';
                table_filed_html += '<td align="left">' + child[ii].value + '</td>';
                table_filed_html += '<td align="left">' + child[ii].label + '</td>';
                table_filed_html += '</tr>';
            }
        }
        $(".layui-table-filed  tbody").html(table_filed_html);
        layui.form.render();
        layui.form.on('checkbox(table_field)', function (data) {
            let checked = 2;
            if (data.elem.checked) {
                checked = 1;
            }
            let post = {
                id: ARR_CONFIG.menu_id,
                type: ARR_CONFIG.type,
                num: data.value || '',
                title: $(data.elem).attr('data-name') || '',
                checked: checked
            }
            layui.http.SendAjax('/ruinuo/design.lists/colsadd', post, [], function (res) {
                layui.table.reload('design_table_elem', {
                    data: res.data, url: '',
                })
                layui.form.render();
            })
        });
    }
    let EditCols = function (arr) {
        let post = {
            id: ARR_CONFIG.menu_id,
            type: ARR_CONFIG.type,
            item: JSON.stringify(arr),
        }
        layui.http.SendAjax('/ruinuo/design.lists/editcols', post, [], function (res) {
            if (res.code != 200) {
                layer.msg(res.msg, {icon: 2});
            } else {
                layer.msg('修改成功', {icon: 1});
                $("input[name='table_field_name']").prop("checked", false);
                arr.forEach(item => {
                    $(".field_val_" + item.field).prop("checked", true);
                })
                layui.form.render();
            }

        })
    }
    let BindTpl = function (obj) {
        let table = layui.table.getRowData(obj);
        ARR_CONFIG.current = table.data || {};
        let area = ['800px', '600px'];
        let content = '/common/component/listtpl';
        let that = '';
        layer.open({
            type: 2,
            offset: 'auto',
            title: '模板配置',
            area: area,
            scrollbar: true,
            content: content,
            btnAlign: 'r',
            btn: ['确定', '取消'],
            shade: 0.3,
            yes: function (index) {
                let iframe = window['layui-layer-iframe' + index];
                let html = iframe.SendForm() || '';
                table.data['tpl'] = html;
                let dataBak = layui.table.cache['design_table_elem'];
                EditCols(dataBak);
                let bindex = layer.getFrameIndex('layui-layer-iframe' + index);
                layer.close(bindex);
            }
        })
    }

    let design_table = {
        Init: Init,//初始化
        ModeList: ModeList,//拖曳完成
        TableFind: TableFind,
        Design: Design,
        ClickMenu: ClickMenu,
        OpenAdd: OpenAdd,
        AddTable: AddTable,
        FieldChecked: FieldChecked,
        EditCols: EditCols,
        GetConfig: GetConfig,
        BindTpl: BindTpl
    };
    exports('design_table', design_table);
})



